package com.cans.lightning.business.lowcode.manager.form.loader.formBean;

import com.cans.lightning.business.lowcode.dto.AppTableDto;
import com.cans.lightning.business.lowcode.manager.form.bean.AppFormBean;
import com.cans.lightning.business.lowcode.manager.form.bean.MainTableBean;
import com.cans.lightning.business.lowcode.manager.form.bean.SonTableBean;
import com.cans.lightning.business.lowcode.manager.form.loader.AbstractBeanLoader;
import com.cans.lightning.business.lowcode.manager.form.loader.tableBean.MainTableBeanLoaderBuilder;
import com.cans.lightning.business.lowcode.service.api.IAppTableService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * formBean 表结构加载
 *  |-- 主表结构加载
 *  |-- 明细表结构加载
 *
 * @author cans
 * @date 2023/11/29
 **/
@Component
public class BeanTableLoader extends AbstractBeanLoader<AppFormBean> {

    @Resource
    private IAppTableService appTableService;
    @Resource
    private MainTableBeanLoaderBuilder mainTableBeanLoaderBuilder;

    @Override
    public AppFormBean loadOne(AppFormBean appFormBean) {
        AppTableDto appTableDto = appTableService.getManTableByFormId(appFormBean.getId());
        MainTableBean mainTableBean = mainTableBeanLoaderBuilder.getLoader().runLoad(new MainTableBean().create(appTableDto));
        appFormBean.setMainTableBean(mainTableBean);
        List<AppTableDto> sonTableDtoList = appTableService.getSonTablesByFormId(appFormBean.getId());
        List<SonTableBean> sonTableBeanList = sonTableDtoList.stream().map(i -> new SonTableBean().create(i)).collect(Collectors.toList());
        appFormBean.setSonTableList(sonTableBeanList);
        return appFormBean;
    }
}
